package humynn.sbd
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import humynn.engine.Entity;
	import humynn.engine.Input;
	import humynn.engine.Renderer;
	
	/**
	 * Used to manage all of the stink bugs in the game.
	 */
	public class StinkBugManager extends Entity
	{
		private var _stinkBugs:Array;
		private var _newBugTimer:Timer;
		private var _defaultDelay:Number;
		
		public function StinkBugManager()
		{
			_stinkBugs = new Array();
			
			_newBugTimer = new Timer(3000);
			_newBugTimer.addEventListener(TimerEvent.TIMER, addBug);
		}
		
		/**
		 * Sets the interval between stink bugs.
		 * 
		 * @param value The new interval.
		 */
		public function set interval(value:Number):void
		{
			_newBugTimer.delay = value;
		}
		
		/**
		 * Sets the delay between stink bug states.
		 * 
		 * @param value The new delay.
		 */
		public function set stateDelay(value:Number):void
		{
			_defaultDelay = value;
		}
		
		/**
		 * Gets the number of stink bugs damaging the health bar.
		 * 
		 * @returns The number of stink bugs dealing damage.
		 */
		public function get stoppedCount():int
		{
			var count:int = 0;
			
			for each(var bug:StinkBug in _stinkBugs)
			{
				if(bug.spraying)
				{
					count++;
				}
			}
			
			return count;
		}
		
		/**
		 * Starts the stink bug spawn timer.
		 */
		public function start():void
		{
			_newBugTimer.start();
		}
		
		/**
		 * Stops the stink bug spawn timer and stops each stink bug.
		 */
		public function stop():void
		{
			_newBugTimer.stop();
			
			for each(var bug:StinkBug in _stinkBugs)
			{
				bug.stop();
			}
		}
		
		/**
		 * Clears out all stink bugs and starts the spawn timer again.
		 */
		public function reset():void
		{
			_stinkBugs = new Array();
			
			_newBugTimer.reset();
			_newBugTimer.start();
		}
		
		/**
		 * Floods the screen with bugs
		 */
		public function flood():void
		{
			var bugsToAdd:int = 100 - _stinkBugs.length;
			
			for(var i:int = 0; i < bugsToAdd; i++)
			{
				addBug(null);
			}
		}
		
		/**
		 * Adds a new stink bug to the game.
		 * 
		 * @param e Ignored.
		 */
		private function addBug(e:TimerEvent):void
		{
			var index:uint = _stinkBugs.length;
			
			_stinkBugs.push(new StinkBug());
			
			_stinkBugs[index].imageFile = _imageFile;
			_stinkBugs[index].x = Math.random() * (800 - 96);
			_stinkBugs[index].y = Math.random() * (600 - 96);
			_stinkBugs[index].rotation = Math.random() * Math.PI * 2;
			_stinkBugs[index].stateDelay = _defaultDelay;
		}
		
		/**
		 * Updates each of the stink bugs.
		 * 
		 * @param input User input.
		 */
		public override function update(input:Input):void
		{
			for each(var bug:StinkBug in _stinkBugs)
			{
				bug.update(input);
			}
		}
		
		/**
		 * Draws each of the stink bugs.
		 * 
		 * @param renderer The renderer to draw to.
		 */
		public override function draw(renderer:Renderer):void
		{
			for each(var bug:StinkBug in _stinkBugs)
			{
				bug.draw(renderer);
			}
		}
	}
}